﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;

namespace HydrogenAtom4 {
    class Util {
        public static bool AlmostZero(double v) {
            return (v <= Double.Epsilon) && (-Double.Epsilon <= v);
        }

        private static Complex SqrtComplex(Complex v) {
            double r = v.Magnitude;
            double θ = v.Phase;
            double real = Math.Sqrt(r) * Math.Cos(θ / 2);
            double imaginary = Math.Sqrt(r) * Math.Sin(θ / 2);
            return new Complex(real, imaginary);
        }

        public static Complex PowComplex(Complex v, int numerator, int denominator) {
            if (denominator != 2 && denominator != 1) {
                throw new NotSupportedException("denominator");
            }

            Complex result = v;
            for (int i = 1; i < numerator; ++i) {
                result *= v;
            }

            if (denominator == 2) {
                return SqrtComplex(result);
            }
            return result;
        }

        public static double Factorial(int x) {
            if (x < 0) {
                throw new ArgumentOutOfRangeException("x");
            }

            int y = 1;
            for (int i = 2; i <= x; ++i) {
                y *= i;
            }

            return y;
        }
    }
}
